/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.graphs.views;

import org.osdtsystem.exceptions.InconsistentViewException;
import org.osdtsystem.exceptions.LayerNotFoundException;
import org.osdtsystem.exceptions.LocationNotFoundException;
import org.osdtsystem.graphs.Graph;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.osdtsystem.exceptions.NonUniqueNameException;
import org.osdtsystem.exceptions.OsdtSyntaxException;
import org.osdtsystem.graphs.io.GraphIDs;
import org.osdtsystem.values.Feature;
import org.osdtsystem.values.Layer;
import org.osdtsystem.values.NodeID;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class NodeCollectionEngine implements NodeCollection {
    private Layer nodeLayer;
    private Graph graph;
    
    /**
     * Create a node collection with the given name in the given graph.
     * @param graph the graph in which the node collection is created.
     * @param name the name of the node collection
     * @throws NonUniqueNameException 
     */
    public NodeCollectionEngine(Graph graph, String name) throws NonUniqueNameException {
        this(graph, graph.addLayer(name + ViewConstants.NodeCollectionSuffix));
        graph.addView(this, name);
    }
    
    public NodeCollectionEngine(Graph graph, Layer nodeLayer) {
        this.graph = graph;
        this.nodeLayer = nodeLayer;
    }

    
    @Override
    public void addNodeFeature(Feature feature) {
        try {
            graph.addLayerFeature(this.nodeLayer, feature);
        } catch (LayerNotFoundException ex) {
            throw new InconsistentViewException("Inconsistent view: node layer not found", ex);
        }
    }

    @Override
    public NodeID addNode() {
        try {
            return graph.addNode(nodeLayer);
        } catch (LayerNotFoundException ex) {
            throw new InconsistentViewException("Inconsistent view: node layer not found", ex);
        }
    }

    @Override
    public List<NodeID> nodes() {
        return graph.nodes(this.nodeLayer);
    }

    @Override
    public List<Feature> nodeFeatures() {
        return graph.features(nodeLayer);
    }

    @Override
    public void clear() {
        graph.clear(nodeLayer);
    }

    @Override
    public void setGraph(Graph graph) {
        this.graph = graph;
    }

    @Override
    public NodeCollection copy() {
        return new NodeCollectionEngine(this.graph, this.nodeLayer);
    }
    
    @Override
    public boolean containsNode(NodeID node) {
        return graph.layer(node) == nodeLayer;
    }


    // 1=view ID, 3=node layer
    private static final Pattern osdtNodeCollectionEnginePattern = Pattern.compile(
        "^\\s*VIEW([0-9]+)\\s+NodeCollectionEngine\\((LAYER)?([0-9]+)\\)\\s*$");

    @Override
    public String toOSDT(Graph graph, GraphIDs ids) {
        return "NodeCollectionEngine(LAYER" + ids.id(nodeLayer) + ")";
    }

    public static NodeCollectionEngine fromOSDT(Graph graph, GraphIDs ids, String osdt)
            throws OsdtSyntaxException {
        // Parse OSDT string
        Matcher m = osdtNodeCollectionEnginePattern.matcher(osdt);
        if (! m.find())
            return null;
        
        // Find view ID
        int viewID = Integer.parseInt(m.group(1));
        
        // Find layer
        Layer layer = ids.getLayer(Integer.parseInt(m.group(3)));
        if (layer == null)
            throw new OsdtSyntaxException("Cannot find layer in OSDT string: " + osdt);
        
        // Return node collection
        return new NodeCollectionEngine(graph, layer);
    }
    
    @Override
    public void recompute() {
        // TODO
    }
}
